package com.shaoshaohuo.app.view;

import android.content.Context;
import android.content.Intent;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.LinearLayout;
import android.widget.RadioButton;

import com.shaoshaohuo.app.R;
import com.shaoshaohuo.app.constant.AppConfig;
import com.shaoshaohuo.app.framework.IViewFramework;
import com.shaoshaohuo.app.ui.ReleaseActivity;
import com.shaoshaohuo.app.ui.fragment.EcCartFragment;
import com.shaoshaohuo.app.ui.fragment.EcMainFragment;
import com.shaoshaohuo.app.ui.fragment.MyInfoFragment;
import com.shaoshaohuo.app.ui.fragment.MarketPriceFragment;

/**
 * 电商主页导航菜单，管理导航标签点击后显示的fragment
 * 
 * @author YinSF
 * @createDate 2015-11-3
 */
public class EcNavigationView extends LinearLayout implements IViewFramework, OnClickListener {
	private RadioButton rbHome;
	private RadioButton rbMarket;
	private RadioButton rbPublish;
	private RadioButton rbCart;
	private RadioButton rbMe;
	private Fragment currentFragment;

	private int currentFragmentViewId = -1;

	public EcNavigationView(Context context) {
		super(context);
		init();
	}

	public EcNavigationView(Context context, AttributeSet attrs) {
		super(context, attrs);
		LayoutInflater.from(context).inflate(R.layout.view_navigation_bottom, this);
		init();
	}

	@Override
	public void init() {
		initView();
		initData();
		initListener();
	}

	@Override
	public void initView() {
		rbHome = (RadioButton) findViewById(R.id.rbHome);
		rbMarket = (RadioButton) findViewById(R.id.rbMarket);
		rbPublish = (RadioButton) findViewById(R.id.rbPublish);
		rbCart = (RadioButton) findViewById(R.id.rbCart);
		rbMe = (RadioButton) findViewById(R.id.rbMe);
	}

	@Override
	public void initData() {
		currentFragmentViewId = rbHome.getId();
	}

	@Override
	public void initListener() {
		rbHome.setOnClickListener(this);
		rbMarket.setOnClickListener(this);
		rbCart.setOnClickListener(this);
		rbMe.setOnClickListener(this);

		rbPublish.setOnTouchListener(new View.OnTouchListener() {
			private boolean down = false;

			@Override
			public boolean onTouch(View v, MotionEvent event) {
				if (event.getAction() == MotionEvent.ACTION_DOWN) {
					down = true;
				} else if (event.getAction() == MotionEvent.ACTION_UP && down) {
					getContext().startActivity(new Intent(getContext(), ReleaseActivity.class));
					down = false;
				} else if (event.getAction() == MotionEvent.ACTION_CANCEL) {
					down = false;
				}
				return true;
			}
		});
	}

	@Override
	public void onClick(View v) {
		int viewId = v.getId();
		if (currentFragmentViewId == viewId) {
			return;
		} else {
			currentFragmentViewId = viewId;
		}
		switch (viewId) {
		case R.id.rbHome:
			changeFragment(EcMainFragment.sharedInstance(""));
			break;
		case R.id.rbCart:
			changeFragment(EcCartFragment.sharedInstance(getContext()));
			break;
		case R.id.rbMe:
			changeFragment(MyInfoFragment.newInstance(""));
			break;
		case R.id.rbMarket:
			changeFragment(MarketPriceFragment.newInstance(""));
			break;

		default:
			break;
		}
	}

	public void changeFragment(Fragment newFragment) {
		FragmentActivity activity = (FragmentActivity) getContext();
		FragmentManager fragmentManager = activity.getSupportFragmentManager();
		FragmentTransaction transaction = fragmentManager.beginTransaction();
		if (AppConfig.FRAGMENT_CUSTOM_TRANSITION_LEFT_OUT != AppConfig.FRAGMENT_NONE && AppConfig.FRAGMENT_CUSTOM_TRANSITION_RIGHT_IN != AppConfig.FRAGMENT_NONE
				&& AppConfig.FRAGMENT_CUSTOM_TRANSITION_LEFT_IN != AppConfig.FRAGMENT_NONE && AppConfig.FRAGMENT_CUSTOM_TRANSITION_RIGHT_OUT != AppConfig.FRAGMENT_NONE) {
			transaction.setCustomAnimations(AppConfig.FRAGMENT_CUSTOM_TRANSITION_RIGHT_IN, AppConfig.FRAGMENT_CUSTOM_TRANSITION_LEFT_OUT,
					AppConfig.FRAGMENT_CUSTOM_TRANSITION_LEFT_IN, AppConfig.FRAGMENT_CUSTOM_TRANSITION_RIGHT_OUT);
		} else {
			transaction.setTransition(AppConfig.FRAGMENT_TRANSITION);
		}
		if (currentFragment == null) {
			transaction.replace(R.id.frame_ec_main, newFragment, newFragment.getClass().getSimpleName()).commit();
		} else {
			if (currentFragment != newFragment) {
				if (newFragment.isAdded()) {
					transaction.hide(currentFragment).show(newFragment).commit();
				} else {
					transaction.hide(currentFragment).add(R.id.frame_ec_main, newFragment, newFragment.getClass().getSimpleName()).show(newFragment).commit();
				}
			}
		}
		currentFragment = newFragment;
	}
}
